package com.ddn.service.personnel.impl;

import com.ddn.mapper.personnel.DeptMapper;
import com.ddn.mapper.personnel.EmplMapper;
import com.ddn.model.personnel.Dept;
import com.ddn.model.personnel.Empl;
import com.ddn.model.personnel.EmplVO;
import com.ddn.service.personnel.EmplService;
import com.ddn.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Bikakaso
 * @date 2022/12/30
 * ddn_hotel
 */
@Service
public class EmplServiceImpl implements EmplService {

    @Autowired
    private EmplMapper emplMapper;
    // 员工
    // 查询全部&&模糊查询
    @Override
    public ResultData findAll(HashMap<String, Object> map) {
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        String ttt = (String) map.get("name");
        map.put("ttt",ttt);
        // 查询所在部门 id

        // 查询岗位id
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        System.out.println("mapservice==== = " + map);
        List<EmplVO> list = emplMapper.findAll(map);
        PageInfo<EmplVO> info = new PageInfo<>(list);
        if (list != null) {
            return ResultData.ok(list,info.getTotal());
        }
        return ResultData.fail();
    }

    // 删除
    @Override
    public ResultData deleteById(int id) {
        int i = emplMapper.deleteById(id);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    // 批量删除
    @Override
    public ResultData deleteBatch(int[] ids) {
        int i = emplMapper.deleteBatch(ids);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    // 添加员工
    @Override
    public ResultData add(Empl empl) {
        if (empl.getSalary() == null) {
            empl.setSalary(0.0);
        }
        if (empl.getHiredate() == null) {
            empl.setHiredate(new Date());
        }
        int i = emplMapper.add (empl);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData edit(Empl empl) {
        int i= emplMapper.edit(empl);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

}
